package com.baizhi.controller;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import com.sun.org.apache.bcel.internal.generic.NEW;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    //把字符串类型转换为日期格式
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); // true:允许输入空值，false:不能为空值

    }

    @Autowired
    private UserService userService;

    //查询所有用户  分页+模糊
    @RequestMapping("/listUser")
    public Map listUser(int page, int limit, String name){
        return userService.listUser(page,limit,name);
    }

    //修改用户状态  冻结或者解冻
    @RequestMapping("/updateUser")
    public Map updateUser(Integer id,Integer status){
        return userService.updateUser(id,status);
    }


    //批量下载用户--批量导出
    @RequestMapping("/multiDownloadUser")
    public void multiDownloadUser() throws IOException {
        ExportParams params = new ExportParams("持明法州项目的注册用户", "user");
        List<User> listUser2 = userService.listUser2();
        Workbook workbook = ExcelExportUtil.exportExcel(params, User.class, listUser2);
        workbook.write(new FileOutputStream("E://user.xls"));
    }


//    优化前
//    男女用户人数对比统计 过去三周用户注册量变化用户省份人数对比
    @RequestMapping("/selectByAll")
    public Map selectByAll(){
        long time1 = new Date().getTime();
        Map maps = userService.selectByAll();
        long time2 = new Date().getTime();
        System.out.println(time2-time1);
        return maps;
    }

//    优化后--多线程多任务并行执行
    @RequestMapping("/selectByThread")
    public Map selectByThread(){
        long time1 = new Date().getTime();
        Map maps = userService.selectByThread();
        long time2 = new Date().getTime();
        System.out.println(time2-time1);
        return maps;
    }
}
